﻿using AZai.AControl;
using AZai.ViewModel;
using ChromeTabs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AZai
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : WindowBase
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// This event triggers when a tab is dragged outside the bonds of the tab control panel.
        /// We can use it to create a docking tab control.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TabControl_TabDraggedOutsideBonds(object sender, TabDragEventArgs e)
        {
            //TabBase draggedTab = e.Tab as TabBase;
            //if (TryDragTabToWindow(e.CursorPosition, draggedTab))
            //{
            //    //Set Handled to true to tell the tab control that we have dragged the tab to a window, and the tab should be closed.
            //    e.Handled = true;
            //}
        }
        protected override bool TryDockWindow(Point position, TabBase dockedWindowVM)
        {
            //Hit test against the tab control
            if (MyChromeTabControl.InputHitTest(position) is FrameworkElement element)
            {
                ////test if the mouse is over the tab panel or a tab item.
                if (CanInsertTabItem(element))
                {
                    //TabBase dockedWindowVM = (TabBase)win.DataContext;
                    ViewModelExampleBase vm = (ViewModelExampleBase)DataContext;
                    vm.ItemCollection.Add(dockedWindowVM);
                    vm.SelectedTab = dockedWindowVM;
                    //We run this method on the tab control for it to grab the tab and position it at the mouse, ready to move again.
                    MyChromeTabControl.GrabTab(dockedWindowVM);
                    return true;
                }
            }
            return false;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        private long lastTitleLeftDownTime = 0;
        /// <summary>
        /// 双击放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            long ticks = DateTime.Now.Ticks / 1000;
            if (ticks - 2000 > lastTitleLeftDownTime)
            {
                lastTitleLeftDownTime = ticks;
            }
            else
            {
                lastTitleLeftDownTime = 0;
                WindowStateChange();
            }
        }
        private void WindowStateChange()
        {
            if (WindowState == WindowState.Normal)
            {
                WindowState = WindowState.Maximized;
            }
            else if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
        }
        private void WrapPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
    }
}
